/**
 * [747] 至少是其他数字两倍的最大数
 *
 * @format
 * @lc app=leetcode.cn id=747 lang=javascript
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @return {number}
 */
var dominantIndex = function (nums) {
    let max = (max2 = index = 0)
    for (let i = 0; i < nums.length; i++) {
        if (nums[i] > max) {
            max2 = max
            max = nums[i]
            index = i
        } else if (nums[i] > max2) {
            max2 = nums[i]
        }
    }
    // 使用乘法降低开销
    return max < max2 * 2 ? -1 : index
    // return max / max2 < 2 ? -1 : index
}
debugger
console.log(dominantIndex([1, 2, 3, 4]))
// var dominantIndex = function (nums) {
//     let num = (index = 0)
//     nums.forEach((e, i) => (e > num ? ((num = e), (index = i)) : null))
//     nums.sort((a, b) => a - b)
//     return nums[nums.length - 1] / nums[nums.length - 2] < 2 ? -1 : index
// }
// @lc code=end
